/*******************************************************************************
 * define Bingo object to offer public function
 * createtime：2017年08月21日
 * version：1.1.0
 * updatetime:2017.09.09
 ******************************************************************************/
$B = Bingo = {};

	//showMessage
	$B.showMessage = function(title, content, cb){
		//如果弹窗不存在
		if(!$("#showMessage-body").length){
			var html = "";
			html += "<div class=\"am-modal am-modal-alert\" tabindex=\"-1\" id=\"showMessage-body\">\n\t\t\t  \t<div class=\"am-modal-dialog\">\n\t\t\t    \t<div class=\"am-modal-hd showMessage-title\">" + title + "</div>\n\t\t\t    \t<div class=\"am-modal-bd showMessage-content\">\n\t\t\t      \t\t" + content + "\n\t\t\t    \t</div>\n\t\t    \t<div class=\"am-modal-footer\">\n\t\t\t      \t<span class=\"am-modal-btn\">确定</span>\n\t\t\t    </div>\n\t\t\t  \t</div>\n\t\t\t</div>";
			$("body").append(html);
		}
		
//		$(".showMessage-title").html(title);
//		$(".showMessage-content").html(content);
		$(".am-modal-dialog").css({"border-radius": "10px"});
		$("#showMessage-body").modal();
		
		return typeof cb == 'function' && cb();
	}

	//showWarning
	$B.showWarning = function(content, cb){
		//如果弹窗不存在
		if(!$("#showWarning-body").length){
			var html = "";
			html += "<div class=\"am-modal am-modal-alert\" tabindex=\"-1\" id=\"showWarning-body\">\n\t\t\t  \t<div class=\"am-modal-dialog\">\n\t\t\t    \t<div class=\"am-modal-hd showWarning-title\"><a class=\"am-badge am-badge-warning am-radius am-text-lg\">Warning</a></div>\n\t\t\t    \t<div class=\"am-modal-bd showWarning-content\">\n\t\t\t      \t\t" + content + "\n\t\t\t    \t</div>\n\t\t\t    \t<div class=\"am-modal-footer\">\n\t\t\t\t      \t<span class=\"am-modal-btn\">\u786E\u5B9A</span>\n\t\t\t\t    </div>\n\t\t\t  \t</div>\n\t\t\t</div>";
			$("body").append(html);
		}
		
//		$(".showWarning-content").html(content);
		$(".am-modal-dialog").css({"border-radius": "10px"});
		$(".am-modal-hd").css({"background": "rgb(243,123,29)",
				"border-top-left-radius": "10px",
				"border-top-right-radius": "10px"});
		$(".am-modal-btn").css({"color": "green"});
		$("#showWarning-body").modal();
		
		return typeof cb == 'function' && cb();
	}
	
	//showError
	$B.showError = function(content, cb){
		if(!$("#showError-body").length){
			var html = "";
			html += "<div class=\"am-modal am-modal-alert\" tabindex=\"-1\" id=\"showError-body\">\n\t\t\t  \t<div class=\"am-modal-dialog\">\n\t\t\t  \t\t\n\t\t\t    \t<div class=\"am-modal-hd showError-title\"><a class=\"am-badge am-badge-danger am-radius am-text-lg\">Danger</a></div>\n\t\t\t    \t<div class=\"am-modal-bd showError-content\">\n\t\t\t      \t\t" + contnent + "\n\t\t\t    \t</div>\n\t\t\t    \t<div class=\"am-modal-footer\">\n\t\t\t\t      \t<span class=\"am-modal-btn\">\u786E\u5B9A</span>\n\t\t\t\t    </div>\n\t\t\t  \t</div>\n\t\t\t</div>";
			$("body").append(html);
		}
		
		$(".am-modal-dialog").css({"border-radius": "10px"});
		$(".am-modal-hd").css({"background": "rgb(221,81,76)",
				"border-top-left-radius": "10px",
				"border-top-right-radius": "10px"});
		$(".am-modal-btn").css({"color": "orange"});
		
//		$(".showError-content").html(content);
		$("#showError-body").modal();
		
		return typeof cb == 'function' && cb();
	}
	
	//showConfirm
	$B.showConfirm = function(title, content, cbTrue, cbFalse){
		if(!$("#showConfirm-body").length){
			var html = "";
			html += "<div class=\"am-modal am-modal-confirm\" tabindex=\"-1\" id=\"showConfirm-body\">\n\t\t\t  \t<div class=\"am-modal-dialog\">\n\t\t\t    \t<div class=\"am-modal-hd showConfirm-title\">" + title + "</div>\n\t\t\t    \t<div class=\"am-modal-bd showConfirm-content\">\n\t\t\t     \t\t " + content + "\n\t\t    \t\t</div>\n\t\t\t    \t<div class=\"am-modal-footer\">\n\t\t\t      \t<span class=\"am-modal-btn\" data-am-modal-cancel>\u53D6\u6D88</span>\n\t\t\t      \t<span class=\"am-modal-btn\" data-am-modal-confirm>\u786E\u5B9A</span>\n\t\t\t    \t</div>\n\t\t\t  \t</div>\n\t\t\t</div>";
			$("body").append(html);
		}
//		$(".showConfirm-title").html(title);
//		$(".showConfirm-content").html(content);
		$('#showConfirm-body').modal({
	        relatedTarget: this,
	        onConfirm: function(options) {
	          	return typeof cbTrue == 'function' && cbTrue();
	        },
	        // closeOnConfirm: false,
	        onCancel: function() {
	         	return typeof cbFalse == 'function' && cbFalse();
	        }
 	 	});
	}

	//request
	$B.REQ = function(url, param, callback, mask){
		//如果需要显示遮罩层
		if(mask == true){
			var html = "";
			html += "<div class='am-modal am-modal-loading am-modal-no-btn' tabindex='-1' id='REQ-loading'>";
			html += "<div class='am-modal-dialog'>";
			html += "<div class='am-modal-bd'>";
			html += "<span class='am-icon-spinner am-icon-spin'></span>";
			html += "</div></div></div>";
			$("body").append(html);
			
			$("#REQ-loading").modal();
		};
		//ajxa请求
		$.ajax({
			type: param.type,
			url: url,
			data: param.data,
			dataType: param.dataType,
			async:true,
			success:function(res){
				return typeof callback == 'function' && callback(res);
			},
			error:function(xhr){
				return typeof callback == 'function' && callback(xhr);
			},
			async:true
		});
	}
	
	//menu
	$B.createMenu = function(button, parent, config, callback){
		var menu = config.content;
		
		var ul = document.createElement("ul");
		var $ul = $(ul);
		$ul.addClass(config.className);
		$ul.addClass("sidebar-nav");
		$ul.attr('id', config.id);
		
		var html = "";
		
		html += "<li class=\"sidebar-nav-heading\"><span class=\"sidebar-nav-heading-info\">" + config.title + "</span></li>";
		
		for(var i = 0;i < menu.length;i++){
			html += "<li class=\"sidebar-nav-link " + menu[i].className + "\">";
			if(menu[i].haveSub){
				html += "<a href=\"javascript:;\" class=\"sidebar-nav-sub-title\"><img class=\"sidebar-nav-link-logo\" src=" + menu[i].icon + "></img>" + menu[i].title + "<span class=\"am-icon-chevron-down am-fr am-margin-right-sm sidebar-nav-sub-ico\"></span></a>";
				for (var j=0;j<menu[i].subMenu.length;j++) {
					if(j==0){
						html += "<ul class=\"sidebar-nav sidebar-nav-sub\">";
					}
					html += "<li class=\"sidebar-nav-link " + menu[i].subMenu[j].className + "\"><a href=" + menu[i].subMenu[j].link + "><span class=\"am-icon-angle-right sidebar-nav-link-logo\"></span>" + menu[i].subMenu[j].title + "</a></li>";
					if(j==(menu[i].subMenu.length-1)){
						html += "</ul>";
					}
				}
			}else{
				html += "<a href=" + menu[i].link + "><img class=\"sidebar-nav-link-logo\" src=" + menu[i].icon + "></img>" + menu[i].title + "</a>";
			}
			html += "</li>";
		}
		
		$ul.append(html);
		$(parent).append($ul);
		
		$(button).on('click',function(){
			$(".sidebar-nav").toggle("normal");
		});
		
		if($(window).width() < 1024){
			$(".sidebar-nav").toggle("normal");
		}
		
		// 侧边菜单
		$('.sidebar-nav-sub-title').on('click', function() {
		    $(this).siblings('.sidebar-nav-sub').slideToggle(80)
		        .end()
		        .find('.sidebar-nav-sub-ico').toggleClass('sidebar-nav-sub-ico-rotate');
		})
		
		return typeof callback == 'function' && callback();
	}

	
	
	